﻿@using KRM.Core
@{
    ViewBag.Title = "Issue Compensation Voucher";
    Layout = "~/Views/Shared/_PopupLayout.cshtml";

    var db = new KRMEntities();

    var reason = db.VoucherReasons.FirstOrDefault(a => a.VoucherReason1 == "Compensation");
    var defaultData = db.VoucherDefaults.FirstOrDefault(a => a.PreFixCode == "COM");

    if (defaultData == null)
    {
        defaultData = new VoucherDefault
        {
            Description = "compensation for:",
            VoucherType = 1,
            RedemptionMethod = 3,
            PreFixCode = "COM",
            ValueType = 2,
            Value = 5,
            ExpiryDays = 100,
            VoucherLayoutID = 3,
            MinimumSpendValue = 0
        };
        db.VoucherDefaults.Add(defaultData);
        db.SaveChanges();
        defaultData = db.VoucherDefaults.OrderByDescending(a => a.DefaultID).FirstOrDefault();

        reason = new VoucherReason
        {
            VoucherReason1 = "Compensation", 
            DefaultID = defaultData.DefaultID, 
            Archive = true
        };
        db.VoucherReasons.Add(reason);
        db.SaveChanges();

    }
    else
    {
        if (reason == null)
        {
            reason = new VoucherReason
            {
                VoucherReason1 = "Compensation",
                DefaultID = defaultData.DefaultID,
                Archive = true
            };
            db.VoucherReasons.Add(reason);
            db.SaveChanges();
        }
    }

    ViewBag.ValueType = new SelectList(db.VoucherValueTypes, "VoucherValueTypeID", "VoucherValueType1", defaultData.ValueType);
    ViewBag.RedemptionMethod = new SelectList(db.VoucherRedemptionMethods, "RedemptionMethodID", "RedemptionMethod", defaultData.RedemptionMethod);
}

<div style="padding: 10px;">
    @Html.Partial("Partial/_MessageResult")
    <form action="/orderview/issuevoucher" method="POST">
        <input type="hidden" name="customerid" value="@ViewBag.Customerid"/>
        <input type="hidden" name="VoucherReasonID" value="@reason.VoucherReasonID"/>
        <input type="hidden" name="DefaultID" value="@defaultData.DefaultID"/>
        <p>Please review the voucher credentials prior to issuing:</p>
        <div class="row-fluid">
            <div class="span4">Value Type</div>
            <div class="span7">
                @Html.DropDownList("ValueType", null, new { @class = "input-block-level" })
            </div>
        </div>
        <div class="row-fluid">
            <div class="span4">Value</div>
            <div class="span7">
                <input type="text" class="input-block-level" name="value" value="@defaultData.Value" required="required" /></div>
        </div>
        <div class="row-fluid">
            <div class="span4">Minimum Spend</div>
            <div class="span7">
                <input type="text" class="input-block-level" name="MinimumSpendValue" value="@defaultData.MinimumSpendValue" required="required" /></div>
        </div>
        <div class="row-fluid">
            <div class="span4">Expiry Date</div>
            <div class="span7">
                <div class="input-append datetimepicker">
                    <input data-format="MM/dd/yyyy HH:mm:ss" type="text" class="input-block-level" style="width: 80%" name="ExpiryDays" required="required" value="@String.Format("{0:M/d/yyyy HH:mm:ss}", DateTime.Now.AddDays((double)defaultData.ExpiryDays))">
                    <span class="add-on">
                        <i data-time-icon="icon-time" data-date-icon="icon-calendar" class="icon-calendar"></i>
                    </span>
                </div>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span4">Redemption Method</div>
            <div class="span7">
                @Html.DropDownList("RedemptionMethod", null, new { @class = "input-block-level" })
            </div>
        </div>
        <p>Further Details for issuing Voucher:</p>
        <div class="row-fluid">
            <textarea class="input-block-level" name="Description">@defaultData.Description</textarea>
        </div>
        <div class="row-fluid">
            <div class="span3 text-right">
                <button type="button" class="btn" onclick="windowclose();">Cancel</button>
            </div>
            <div class="span1"></div>
            <div class="span3">
                <button type="submit" class="btn btn-primary">Issue Voucher</button>
            </div>
        </div>
    </form>
</div>
